<!--区域酒店完整选择-->
<template>
  <el-cascader ref="areas" v-model="regions" :props="props" :options="areas" collapse-tags clearable @change="change"></el-cascader>
</template>

<script lang="ts">
import { getRegion } from '@/api/system/region/index';
import { listVendor } from '@/api/vendor/index';

export default {
  props: {
    /**当前双向数据绑定的值 */
    modelValue: {
      type: [String, Number],
      default: ''
    },
    /**输入框内部的文字 */
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  computed: {
    regionValues: {
      get() {
        return this.regions;
      },
      set(value) {
        this.regions = value;
      }
    }
  },
  data() {
    return {
      regions: [2],
      type: 1,
      areas: [
        { label: '全局', value: 1, leaf: true },
        { label: '区域', value: 3 },
        { label: '酒店', value: 2 }
      ],
      props: {
        multiple: true,
        lazy: true,
        // emitPath: false,

        lazyLoad(node, resolve) {
          let code = node.value;
          if (node.level == 1) {
            let type = node.value;
            this.type = type;
            code = null;
          }

          switch (this.type) {
            case 1:
              resolve([]);
              break;
            case 3:
              getRegion(code).then((res) => {
                let nodes = res.data.map((it) => ({
                  value: it.code,
                  label: it.name,
                  leaf: node.level >= 3
                }));
                resolve(nodes);
              });
              break;
            case 2:
              if (node.level == 1)
                listVendor({ name: '', pageNum: 1, pageSize: 2000000 }).then((res) => {
                  let datas = res.rows;
                  let nodes = datas.map((it) => ({
                    value: it.vendorId,
                    label: it.name,
                    leaf: true
                  }));
                  resolve(nodes);
                });
              break;
          }
        }
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.regions = JSON.parse(JSON.stringify(this.modelValue));

      // //上面的代码自行补充
      // this.$nextTick((_) => {
      //   var node = this.$refs.areas.getCheckedNodes(); //获取当前选中节点
      //   if (node && node[0] && node[0].pathNodes) {
      //     //选中节点的所有父节点
      //     for (var i = 0; i < node[0].pathNodes.length; i++) {
      //       node[0].pathNodes[i].loaded = true; //节点的加载状态设为true，就不会再加载了
      //     }
      //   }
      // });
    },
    change(datas) {
      debugger;
      this.$emit('update:model-value', datas);
    }
  }
};
</script>
